const board = document.getElementById('board');
const rows = 15;
const cols = 15;
let currentPlayer = 'black';

// 创建棋盘
for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
        const cell = document.createElement('div');
        cell.classList.add('cell');
        cell.dataset.row = i;
        cell.dataset.col = j;
        cell.addEventListener('click', handleCellClick);
        board.appendChild(cell);
    }
}

function handleCellClick() {
    if (this.classList.contains('black') || this.classList.contains('white')) {
        return;
    }
    this.classList.add(currentPlayer);

    const row = this.dataset.row;
    const col = this.dataset.col;

    // 发送落子信息到后端
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'game.php', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            console.log(xhr.responseText);
        }
    };
    xhr.send(`row=${row}&col=${col}&player=${currentPlayer}`);

    // 切换玩家
    currentPlayer = currentPlayer === 'black' ? 'white' : 'black';
}